package com.hyt.it.ogt.kq.main.controller.gov;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.core.util.StringUtils;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.common.gov.enums.OfficeFileTemplateTypeEnum;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.service.gov.model.entity.BreakRuleBehavior;
import com.hyt.it.ogt.kq.service.gov.model.entity.BreakRuleScheme;
import com.hyt.it.ogt.kq.service.gov.model.entity.Task;
import com.hyt.it.ogt.kq.service.gov.model.vo.BreakRuleBehaviorVO;
import com.hyt.it.ogt.kq.service.gov.model.vo.SchemeListVO;
import com.hyt.it.ogt.kq.service.gov.service.IBreakRuleBehaviorService;
import com.hyt.it.ogt.kq.service.gov.service.IBreakRuleSchemeService;
import com.hyt.it.ogt.kq.service.gov.service.IOfficeFileTemplateService;
import com.hyt.it.ogt.kq.service.gov.service.ITaskService;
import com.hyt.model.PageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 违纪信息管理
 * </p>
 *
 * @author huangyh
 * @since 2021-03-08
 */
@RestController
@Api(tags = "103.违纪接口", value = "违纪接口")
@ApiSort(value = 103)
@RequestMapping("/kw/gov/breakRule")
@Slf4j
public class BreakRuleController extends BaseController {

    @Resource
    private ITaskService iTaskService;

    @Resource
    private IBreakRuleSchemeService iBreakRuleSchemeService;

    @Resource
    private IBreakRuleBehaviorService iBreakRuleBehaviorService;

    @Resource
    private IOfficeFileTemplateService iOfficeFileTemplateService;

    /**
     * 下载违纪信息基础模板
     */
    @ApiOperation(value = "3.1 获取下载违纪信息基础模板路径")
    @ApiOperationSort(value = 1)
    @GetMapping("/getDownloadTemplateUrl")
    public ApiResponse<String> getDownloadTemplateUrl(@RequestParam String taskId) {
        log.debug("{}", taskId);
        try {
            String url = iOfficeFileTemplateService.getUrl(super.getOfficeId(),
                    OfficeFileTemplateTypeEnum.TEMPLATE_BREAK_RULE.getName(),
                    OfficeFileTemplateTypeEnum.TEMPLATE_BREAK_RULE.getName());

            return ApiResponse.<String>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(url)
                    .build();
        } catch (KqException i) {
            log.error("# 获取下载违纪信息基础模板路径异常: {}", i.getMessage());
            return ApiResponse.<String>builder()
                    .code(i.getErrCode()).data(i.getErrMsg())
                    .build();
        } catch (Exception e) {
            log.error("# 获取下载违纪信息基础模板路径异常:", e);
            return ApiResponse.<String>builder()
                    .code(ResponseCode.ERROR_GET_DOWNLOAD_TEMPLATE_URL.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "3.2 违纪信息导入",
            notes = "30000:违纪信息导入成功; 30021:违纪信息导入异常; 30010:导入违纪信息文件类型错误; 30033:考试任务id不能为空"
    )
    @ApiOperationSort(value = 2)
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ApiResponse<Object> importExcel(@RequestPart(value = "file") MultipartFile file, String taskId) {
        log.info("# 违纪信息导入参数 ： {}", taskId);
        try {
            if (StringUtils.isEmpty(taskId)
                    || iTaskService.count(Wrappers.<Task>lambdaQuery().eq(Task::getId, taskId)) == 0) {
                //考试任务id不能为空
                return ApiResponse.builder().code(ResponseCode.TASK_ID_ISNULL_OR_EXIST.getCode()).build();
            }
            iBreakRuleSchemeService.importExcel(file, taskId);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (KqException i) {
            log.error("# 违纪信息导入异常: {}", i.getMessage());
            return ApiResponse.builder()
                    .code(i.getErrCode()).data(i.getErrMsg())
                    .build();
        } catch (Exception e) {
            log.error("# 违纪信息导入异常:", e);
            return ApiResponse.builder()
                    .code(ResponseCode.ERROR_BREAK_RULE_IMPORT.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "3.3 查询违纪行为信息分页",
            notes = "30000:查询违纪行为信息成功; 30044:查询违纪行为信息异常;"
    )
    @ApiOperationSort(value = 3)
    @GetMapping("/selectPageBreakRuleBehavior")
    public ApiResponse<Page<BreakRuleBehaviorVO>> selectPageBreakRuleBehavior(PageParam<BreakRuleBehaviorVO> pageParam,
                                                                              @RequestParam String taskId, String key) {
        log.info("# 通过考试任务id查询所有违纪信息参数 ： {}", taskId);
        Integer code = ResponseCode.SUCCESS.getCode();
        Page<BreakRuleBehaviorVO> data = null;
        try {
            if (key != null) {
                key = key.trim();
            }
            data = iBreakRuleSchemeService.selectPageBreakRuleBehavior(pageParam, taskId, key);
        } catch (Exception e) {
            log.error("# 通过考试任务id({})查询所有违纪信息异常:", taskId, e);
            code = ResponseCode.BEHAVIOR_QUERY_EXCEPTION_BY_ID.getCode();
        }
        return ApiResponse.<Page<BreakRuleBehaviorVO>>builder().code(code).data(data).build();
    }

    @ApiOperation(
            value = "3.4 删除违纪处理方法",
            notes = "30000:删除违纪处理方法成功; 30049:删除违纪处理方法异常; 30050:考试任务id或违纪处理方法ids不能为空"
    )
    @ApiOperationSort(value = 4)
    @DeleteMapping("/deleteScheme")
    public ApiResponse<Object> deleteScheme(@RequestParam String ids, @RequestParam String taskId) {
        log.info("# 删除违纪处理方法参数 ： {}, {}", ids, taskId);
        try {
            //参数校验
            if (StringUtils.isEmpty(taskId) || StringUtils.isEmpty(ids)) {
                return ApiResponse.builder().code(ResponseCode.BEHAVIOR_TASK_OR_ID_IS_EMPTY.getCode()).build();
            }
            iBreakRuleSchemeService.deleteScheme(ids, taskId);
            return ApiResponse.builder().code(ResponseCode.SUCCESS.getCode()).build();
        } catch (KqException k) {
            log.error("# 删除违纪处理方法业务异常: {}", k.getMessage());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .build();
        } catch (Exception e) {
            log.error("# 删除违纪处理方法异常:", e);
            return ApiResponse.builder().code(ResponseCode.BEHAVIOR_DELETE_SCHEME_ERROR.getCode()).build();
        }
    }

    @ApiOperation(
            value = "3.5 删除违纪行为",
            notes = "30000:删除违纪行为成功; 30052:删除违纪行为异常; 30045:考试任务id不能为空"
    )
    @ApiOperationSort(value = 5)
    @DeleteMapping("/deleteBehavior")
    public ApiResponse<Object> deleteBehavior(@RequestParam String ids) {
        log.info("# 删除违纪行为参数 ： {}", ids);
        try {
            //参数校验
            if (StringUtils.isEmpty(ids)) {
                return ApiResponse.builder().code(ResponseCode.BEHAVIOR_ID_IS_EMPTY.getCode()).build();
            }
            iBreakRuleBehaviorService.deleteBehavior(ids);
            return ApiResponse.builder().code(ResponseCode.SUCCESS.getCode()).build();
        } catch (KqException k) {
            log.error("# 删除违纪行为业务异常: {}", k.getMessage());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .build();
        } catch (Exception e) {
            log.error("# 删除违纪处理方法异常:", e);
            return ApiResponse.builder().code(ResponseCode.BEHAVIOR_DELETE_ERROR.getCode()).build();
        }
    }

    @ApiOperation(
            value = "3.6 查询违纪处理方法下拉列表",
            notes = "30000:查询违纪处理方法下拉列表成功; 30055:查询违纪处理方法下拉列表异常; 30045:考试任务id不能为空"
    )
    @ApiOperationSort(value = 6)
    @GetMapping("/queryScheme")
    public ApiResponse<List<SchemeListVO>> queryScheme(@RequestParam String taskId) {
        log.info("# 查询违纪处理方法下拉列表参数 ： {}", taskId);
        try {
            if (StringUtils.isEmpty(taskId)) {
                return ApiResponse.<List<SchemeListVO>>builder().code(ResponseCode.BEHAVIOR_TASK_ID_IS_EMPTY.getCode()).build();
            }
            List<SchemeListVO> schemeList = iBreakRuleSchemeService.queryScheme(taskId);
            return ApiResponse.<List<SchemeListVO>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(schemeList)
                    .build();
        } catch (Exception e) {
            log.error("# 查询违纪处理方法下拉列表异常:", e);
            return ApiResponse.<List<SchemeListVO>>builder()
                    .code(ResponseCode.ERROR_QUERY_SCHEME_LIST.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "3.7 新增违纪处理方法",
            notes = "30000:新增违纪处理方法成功; 30056:新增违纪处理方法异常; 30057:新增违纪处理方法参数校验错误; 30058:当前考试任务下存在相同的违纪处理代码; 30059:停考年限只能是非负整数字; 30060:违纪处理方法代码只能是1~3位的数字"
    )
    @ApiOperationSort(value = 7)
    @RequestMapping(value = "/addScheme", method = RequestMethod.POST)
    public ApiResponse<Object> addScheme( @Valid @RequestBody BreakRuleScheme breakRuleScheme ) {
        log.info("# 新增违纪处理方法参数 ： {}", breakRuleScheme.toString());
        try {
            breakRuleScheme.setId(null);
            iBreakRuleSchemeService.addScheme(breakRuleScheme);
            return ApiResponse.builder().code(ResponseCode.SUCCESS.getCode()).build();
        } catch (KqException i) {
            log.error("# 新增违纪处理方法异常: {}", i.getMessage());
            return ApiResponse.builder().code(i.getErrCode()).data(i.getErrMsg()).build();
        } catch (Exception e) {
            log.error("# 新增违纪处理方法异常:", e);
            return ApiResponse.builder().code(ResponseCode.BEHAVIOR_ERROR_ADD_SCHEME_EXCEPTION.getCode()).build();
        }
    }

    @ApiOperation(
            value = "3.8 修改违纪处理方法",
            notes = "30000:修改违纪处理方法成功; 30061:修改违纪处理方法异常; 30062:修改违纪处理方法参数校验错误; 30058:当前考试任务下存在相同的违纪处理代码; 30059:停考年限只能是非负整数字; 30060:违纪处理方法代码只能是1~3位的数字"
    )
    @ApiOperationSort(value = 8)
    @PutMapping("/updateScheme")
    public ApiResponse<Object> updateScheme(@RequestBody @Valid BreakRuleScheme param) {
        log.info("# 修改违纪处理方法参数 ： {}", param.toString());
        try {
            iBreakRuleSchemeService.updateScheme(param);
            return ApiResponse.builder().code(ResponseCode.SUCCESS.getCode()).build();
        } catch (KqException i) {
            log.error("# 修改违纪处理方法异常: {}", i.getMessage());
            return ApiResponse.builder().code(i.getErrCode()).data(i.getErrMsg()).build();
        } catch (Exception e) {
            log.error("# 修改违纪处理方法异常:", e);
            return ApiResponse.builder().code(ResponseCode.BEHAVIOR_UPDATE_SCHEME_EXCEPTION.getCode()).build();
        }
    }

    @ApiOperation(
            value = "3.9 新增违纪行为",
            notes = "30000:新增违纪行为成功; 30063:新增违纪行为异常; 30064:新增违纪行为参数校验错误; 30065:当前考试任务下存在相同的违纪行为代码; 30066:违纪行为代码只能是1~3位的数字; 30067:违纪处理方法不存在"
    )
    @ApiOperationSort(value = 9)
    @PostMapping("/addBehavior")
    public ApiResponse<Object> addBehavior(@RequestBody @Valid BreakRuleBehavior param) {
        log.info("# 新增违纪行为参数 ： {}", param.toString());
        try {
            param.setId(null);
            iBreakRuleBehaviorService.addBehavior(param);
            return ApiResponse.builder().code(ResponseCode.SUCCESS.getCode()).build();
        } catch (KqException i) {
            log.error("# 新增违纪行为异常: {}", i.getMessage());
            return ApiResponse.builder().code(i.getErrCode()).data(i.getErrMsg()).build();
        } catch (Exception e) {
            log.error("# 新增违纪行为异常:", e);
            return ApiResponse.builder().code(ResponseCode.BEHAVIOR_ADD_EXCEPTION.getCode()).build();
        }
    }

    @ApiOperation(
            value = "3.10 修改违纪行为",
            notes = "30000:修改违纪行为成功; 30068:修改违纪行为异常; 30069:修改违纪行为参数校验错误; 30065:当前考试任务下存在相同的违纪行为代码; 30066:违纪行为代码只能是1~3位的数字; 30067:违纪处理方法不存在"
    )
    @ApiOperationSort(value = 10)
    @PutMapping(value = "/updateBehavior")
    public ApiResponse<Object> updateBehavior(@RequestBody @Valid BreakRuleBehavior param) {
        log.info("# 修改违纪行为参数 ： {}", param.toString());
        try {
            iBreakRuleBehaviorService.updateBehavior(param);
            return ApiResponse.builder().code(ResponseCode.SUCCESS.getCode()).build();
        } catch (KqException i) {
            log.error("# 修改违纪行为异常: {}", i.getMessage());
            return ApiResponse.builder()
                    .code(i.getErrCode()).data(i.getErrMsg())
                    .build();
        } catch (Exception e) {
            log.error("# 修改违纪行为异常:", e);
            return ApiResponse.builder().code(ResponseCode.BEHAVIOR_UPDATE_EXCEPTION.getCode()).build();
        }
    }

    @ApiOperation(
            value = "3.11 查询违纪处理方法信息分页",
            notes = "30000:成功; 30055:异常;"
    )
    @ApiOperationSort(value = 11)
    @GetMapping("/selectPageBreakRuleScheme")
    public ApiResponse<Page<BreakRuleScheme>> selectPageBreakRuleScheme(PageParam<BreakRuleScheme> pageParam,
                                                                        @RequestParam String taskId, String key) {
        if (StringUtils.isEmpty(taskId)) {
            return ApiResponse.<Page<BreakRuleScheme>>builder()
                    .code(ResponseCode.BEHAVIOR_TASK_ID_IS_EMPTY.getCode())
                    .build();
        }
        log.info("# 通过考试任务id违纪处理方法参数 ： {}", taskId);
        Integer code = ResponseCode.SUCCESS.getCode();
        Page<BreakRuleScheme> data = null;
        try {
            if (key != null) {
                key = key.trim();
            }
            data = iBreakRuleSchemeService.selectPageBreakRuleScheme(pageParam, taskId, key);
        } catch (Exception e) {
            log.error("# 通过考试任务id({})违纪处理方法异常:", taskId, e);
            code = ResponseCode.ERROR_QUERY_SCHEME.getCode();
        }
        return ApiResponse.<Page<BreakRuleScheme>>builder().code(code).data(data).build();
    }
}
